package com.hand.demo.api.controller.v1;

import org.hzero.core.util.Results;
import org.hzero.core.base.BaseController;
import com.hand.demo.domain.entity.XzyExample;
import com.hand.demo.domain.repository.XzyExampleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.hzero.mybatis.helper.SecurityTokenHelper;
import com.hand.demo.app.service.XzyExampleService;

import io.choerodon.core.domain.Page;
import io.choerodon.core.iam.ResourceLevel;
import io.choerodon.mybatis.pagehelper.annotation.SortDefault;
import io.choerodon.mybatis.pagehelper.domain.PageRequest;
import io.choerodon.mybatis.pagehelper.domain.Sort;
import io.choerodon.swagger.annotation.Permission;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;
import io.swagger.annotations.ApiParam;

import java.util.List;

/**
 * example表的设计 管理 API
 *
 * @author zeyu.xiao@hand-china.com 2023-09-21 14:51:43
 */
@RestController("xzyExampleController.v1")
@RequestMapping("/v1/{organizationId}/xzy-examples")
public class XzyExampleController extends BaseController {

    @Autowired
    private XzyExampleRepository xzyExampleRepository;
    @Autowired
    private XzyExampleService xzyExampleService;

    @ApiOperation(value = "example表的设计维护-分页查询example表的设计列表")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @GetMapping
    public ResponseEntity<Page<XzyExample>> page(@PathVariable(value = "organizationId") Long organizationId,
                                                 XzyExample xzyExample,
                                                 @ApiIgnore @SortDefault(value = XzyExample.FIELD_ID,
                                                         direction = Sort.Direction.DESC) PageRequest pageRequest) {
        Page<XzyExample> list = xzyExampleRepository.pageAndSort(pageRequest, xzyExample);
        return Results.success(list);
    }

    @ApiOperation(value = "example表的设计维护-查询example表的设计明细")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @GetMapping("/{id}")
    public ResponseEntity<XzyExample> detail(@PathVariable(value = "organizationId") Long organizationId,
                                             @ApiParam(value = "example表的设计ID", required = true) @PathVariable Long id) {
        XzyExample xzyExample = xzyExampleRepository.selectByPrimaryKey(id);
        return Results.success(xzyExample);
    }

    @ApiOperation(value = "example表的设计维护-创建example表的设计")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @PostMapping
    public ResponseEntity<XzyExample> create(@PathVariable(value = "organizationId") Long organizationId,
                                             @RequestBody XzyExample xzyExample) {
        validObject(xzyExample);
        xzyExampleService.save(xzyExample);
        return Results.success(xzyExample);
    }

    @ApiOperation(value = "example表的设计维护-修改example表的设计")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @PutMapping
    public ResponseEntity<XzyExample> update(@PathVariable(value = "organizationId") Long organizationId,
                                             @RequestBody XzyExample xzyExample) {
        SecurityTokenHelper.validToken(xzyExample);
        xzyExampleService.save(xzyExample);
        return Results.success(xzyExample);
    }

    @ApiOperation(value = "example表的设计维护-批量保存example表的设计")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @PostMapping("/batch-saving")
    public ResponseEntity<List<XzyExample>> batchSave(@PathVariable(value = "organizationId") Long organizationId,
                                                      @RequestBody List<XzyExample> xzyExampleList) {
        SecurityTokenHelper.validToken(xzyExampleList);
        xzyExampleService.batchSave(xzyExampleList);
        return Results.success(xzyExampleList);
    }

    @ApiOperation(value = "example表的设计维护-删除example表的设计")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @DeleteMapping
    public ResponseEntity<Void> remove(@PathVariable(value = "organizationId") Long organizationId,
                                       @RequestBody XzyExample xzyExample) {
        SecurityTokenHelper.validToken(xzyExample);
        xzyExampleRepository.deleteByPrimaryKey(xzyExample);
        return Results.success();
    }

}
